package com.yyyzzz.servlet;

import com.yyyzzz.bean.Admin;
import com.yyyzzz.service.adminService;
import com.yyyzzz.service.impl.adminServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/adminLoginServlet")
public class adminLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String adminname = request.getParameter("adminname");
        String password = request.getParameter("password");
        HttpSession session =request.getSession();
        if (adminname==null|| "".equals(adminname.trim())){
            session.setAttribute("message","请输入用户名");
            request.getRequestDispatcher("adminLogin.jsp").forward(request,response);
        }
        if (password==null|| "".equals(password.trim())){
            session.setAttribute("message","请输入密码");
            request.getRequestDispatcher("adminLogin.jsp").forward(request,response);
        }
        adminService adminservice=new adminServiceimpl();
        Admin admin = new Admin(adminname,password);
        boolean isLogin=adminservice.isLogin(admin);
        if(isLogin==false){
            session.setAttribute("message","用户不存在请先注册");
            response.sendRedirect(request.getContextPath()+"/adminLogin.jsp");
        }else{
            response.sendRedirect(request.getContextPath()+"/courseDetail.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

